import { defineStore } from 'pinia'
import router from "@/router";

export const useUserStore = defineStore('userInfo', {
    state: () => ({
        managerInfo: {}
    }),
    getters: {
        getUserId() {
            return this.managerInfo.userInfo ? this.managerInfo.userInfo.id : 0
        },
        getUser() {
            return this.managerInfo.userInfo || {}
        },
        getBearerToken() {
            return this.managerInfo.token ? 'Bearer ' + this.managerInfo.token : ''
        },
        getToken() {
            return this.managerInfo.token || ""
        },
        getMenus(){
            return this.managerInfo.menuList;
        },
        getAuths() {
            return this.managerInfo.auths.length ? this.managerInfo.auths.map(v => v.auth) : []
        }
    },
    actions: {
        setManagerInfo(managerInfo) {
            this.managerInfo = managerInfo
        },
        setUser(user) {
            this.managerInfo.userInfo = JSON.parse(JSON.stringify(user))
        },
        logout() {
            localStorage.removeItem('userInfo')
            router.push('/login')
        }
    },
    // 开启数据持久化
    persist: true
})
